/******************************************************************
 * @version      : 1.0
 * @Author       : zhl
 * @Date         : 2023-01-20 11:07:43
 * @LastEditors  : zhl
 * @E-Mail       : zhll168@163.com
 * @Description  : 业务描述
 ******************************************************************/
#ifndef __JSON_TRANSLATOR_H__
#define __JSON_TRANSLATOR_H__

#include <string>
#include <iostream>
#include <unordered_map>
#include <typeinfo>
#include <iostream>
#include <unistd.h>
#include <cstddef>
#include <sys/timeb.h>
#include <type_traits>
#include <typeinfo>
#include <regex>
#include "json.hpp"
#include "common.h"
#include "CLogger.h"
#include "protocol_engine_config.h"
#include "global_data_struct.h"


using json = nlohmann::json;

/// @brief 数据类型一致性检查
/// @tparam T 
/// @param obj 
/// @param type 
/// @return 
template<typename T>
bool consistent_check(T& obj, json::value_t type){
    if((type == json::value_t::null) || (type == json::value_t::binary)
        || (type == json::value_t::discarded)){
        return false;
    } else if((type == json::value_t::array) || (type == json::value_t::object)){
        return true;
    } else if(type == json::value_t::string){
        if(std::is_same<typename std::decay<T>::type,std::string>::value
            || std::is_same<typename std::decay<T>::type,char*>::value){
            return true;
        } else {
            return false;
        }
    } else {
        if(std::is_same<typename std::decay<T>::type,std::string>::value
            || std::is_same<typename std::decay<T>::type,char*>::value){
            return false;
        } else {
            return true;
        }
    }
}

template<typename T>
void json_to_obj(const json& j, std::string field, T& obj){
    if(field.empty()){
        return ;
    }
    if(j.contains(field)){
        if(consistent_check(obj, j.at(field).type())){
            j.at(field).get_to(obj);
        } else {
            CLogger::getInstance()->log_w("json解析，字段%s(%s:%s)的数据类型不匹配！", field.c_str(), j.at(field).type_name(), typeid(T).name());
        }
    }
}

void to_json(json& j, const DLT698_DATA_TYPE& obj) {
    j = json{
        {"code",obj.code}, 
        {"dataType", obj.data_type}, 
        {"name", obj.name}, 
        {"size", obj.size}, 
        {"equalSystemDataType", obj.equal_system_data_type}
    };
}

void from_json(const json& j, DLT698_DATA_TYPE& obj) {
#if 0
    j.at("code").get_to(obj.code);
    j.at("dataType").get_to(obj.data_type);
    j.at("name").get_to(obj.name);
    j.at("size").get_to(obj.size);
    j.at("equalSystemDataType").get_to(obj.equal_system_data_type);
    j.at("subItemNo").get_to(obj.sub_item_no);
#endif
    json_to_obj<int>(j, "code", obj.code);
    json_to_obj<std::string>(j, "dataType", obj.data_type);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "size", obj.size);
    json_to_obj<int>(j, "equalSystemDataType", obj.equal_system_data_type);
    //json_to_obj<int>(j, "subItemNo", obj.sub_item_no);
}
void to_json(json& j, const std::list<DLT698_DATA_TYPE>& objs) {
    for(auto data_type = objs.begin(); data_type != objs.end(); data_type++){
        j.push_back(*data_type);
    }
}

void from_json(const json& j, std::list<DLT698_DATA_TYPE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT698_DATA_TYPE data_type = *jj;
        objs.push_back(data_type);
    }
}

void to_json(json& j, const DLT645_DATA_TYPE& obj) {
    j = json{
        {"id",obj.id}, 
        {"type", obj.data_type}, 
        {"format", obj.format}, 
        {"size", obj.size}, 
        {"desc", obj.desc}, 
    };
}

void from_json(const json& j, DLT645_DATA_TYPE& obj) {
#if 0
    j.at("id").get_to(obj.code);
    j.at("type").get_to(obj.data_type);
    j.at("format").get_to(obj.format);
    //j.at("size").get_to(obj.size);
    j.at("desc").get_to(obj.desc);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "format", obj.format);
    json_to_obj<std::string>(j, "desc", obj.desc);
    json_to_obj<int>(j, "size", obj.size);
    json_to_obj<int>(j, "type", obj.data_type);
}
void to_json(json& j, const std::list<DLT645_DATA_TYPE>& objs) {
    for(auto data_type = objs.begin(); data_type != objs.end(); data_type++){
        j.push_back(*data_type);
    }
}

void from_json(const json& j, std::list<DLT645_DATA_TYPE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT645_DATA_TYPE data_type = *jj;
        objs.push_back(data_type);
    }
}

void to_json(json& j, const PROTOCOL_RULE& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.protocol_name},
        {"type", obj.protocol_type},
        {"timeout", obj.timeout}
    };
}

void from_json(const json& j, PROTOCOL_RULE& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.protocol_name);
    j.at("type").get_to(obj.protocol_type);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.protocol_name);
    json_to_obj<int>(j, "type", obj.protocol_type);
    json_to_obj<int>(j, "timeout", obj.timeout);
    if(1 > obj.timeout){
        obj.timeout = 3000;
    }
}
//
void to_json(json& j, const std::list<PROTOCOL_RULE>& objs) {
    for(auto rule = objs.begin(); rule != objs.end(); rule++){
        j.push_back(*rule);
    }
}
//
void from_json(const json& j, std::list<PROTOCOL_RULE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        PROTOCOL_RULE rule = *jj;
        objs.push_back(rule);
    }
}
//串口
void to_json(json& j, const SERIAL_INFO& obj) {
    j = json{
        {"id", obj.id},
        {"ifAddr", obj.addr},
        {"baudRate", obj.baud_rate},
        {"dataSize", obj.data_size},
        {"stopBit", obj.stop_bit},
        {"parity", obj.parity}
    };
}
//
void from_json(const json& j, SERIAL_INFO& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("ifAddr").get_to(obj.addr);
    j.at("baudRate").get_to(obj.baud_rate);
    j.at("dataSize").get_to(obj.data_size);
    j.at("stopBit").get_to(obj.stop_bit);
    j.at("parity").get_to(obj.parity);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "ifAddr", obj.addr);
    json_to_obj<int>(j, "baudRate", obj.baud_rate);
    json_to_obj<int>(j, "dataSize", obj.data_size);
    json_to_obj<int>(j, "stopBit", obj.stop_bit);
    json_to_obj<std::string>(j, "parity", obj.parity);
}
void to_json(json& j, const std::list<SERIAL_INFO>& objs) {
    for(auto serial = objs.begin(); serial != objs.end(); serial++){
        j.push_back(*serial);
    }
}
//
void from_json(const json& j, std::list<SERIAL_INFO>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        SERIAL_INFO serial = *jj;
        objs.push_back(serial);
    }
}
//net
void to_json(json& j, const NET_INFO& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"ipType", obj.ip_type},
        {"ip", obj.ip_addr},
        {"port", obj.port}
    };
}
//
void from_json(const json& j, NET_INFO& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("ipType").get_to(obj.ip_type);
    j.at("ip").get_to(obj.ip_addr);
    j.at("port").get_to(obj.port);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "ipType", obj.ip_type);
    json_to_obj<std::string>(j, "ip", obj.ip_addr);
    json_to_obj<uint16_t>(j, "port", obj.port);
}
void to_json(json& j, const std::list<NET_INFO>& objs) {
    for(auto net = objs.begin(); net != objs.end(); net++){
        j.push_back(*net);
    }
}
//
void from_json(const json& j, std::list<NET_INFO>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        NET_INFO net = *jj;
        objs.push_back(net);
    }
}
//HPLC
void to_json(json& j, const HPLC_INFO& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"frequency", obj.frequency}
    };
}

void from_json(const json& j, HPLC_INFO& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("frequency").get_to(obj.frequency);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "ipType", obj.frequency);
}
void to_json(json& j, const std::list<HPLC_INFO>& objs) {
    for(auto hplc = objs.begin(); hplc != objs.end(); hplc++){
        j.push_back(*hplc);
    }
}
//
void from_json(const json& j, std::list<HPLC_INFO>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        HPLC_INFO hplc = *jj;
        objs.push_back(hplc);
    }
}
//
void to_json(json& j, const EDGE_DEVICE& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"sn", obj.sn},
        {"dataReportType", obj.data_report_type},
        {"reportPeriod", obj.report_period},
        {"dataDepth", obj.data_depth},
        {"serials", obj.serials},
        {"nets", obj.nets},
        {"hplcs", obj.hplcs}
    };
}
//
void from_json(const json& j, EDGE_DEVICE& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("alertCount").get_to(obj.alert_count);
    j.at("dataReportType").get_to(obj.data_report_type);
    j.at("reportPeriod").get_to(obj.report_period);
    j.at("dataDepth").get_to(obj.data_depth);
    j.at("serials").get_to(obj.serials);
    j.at("nets").get_to(obj.nets);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<std::string>(j, "sn", obj.sn);
    json_to_obj<int>(j, "dataReportType", obj.data_report_type);
    json_to_obj<int>(j, "reportPeriod", obj.report_period);
    json_to_obj<int>(j, "dataDepth", obj.data_depth);
    json_to_obj<std::list<SERIAL_INFO>>(j, "serials", obj.serials);
    json_to_obj<std::list<NET_INFO>>(j, "nets", obj.nets);
    json_to_obj<std::list<HPLC_INFO>>(j, "hplcs", obj.hplcs);

}
//
void to_json(json& j, const std::list<EDGE_DEVICE>& objs) {
    auto dev = objs.begin();
    if(dev == objs.end()){
        return ;
    }
    j.push_back(*dev);
}
//
void from_json(const json& j, std::list<EDGE_DEVICE>& objs) {
    EDGE_DEVICE edge_dev = j;
    objs.push_back(edge_dev);
}
//
void to_json(json& j, const TERMINAL_DEVICE& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"protocolId", obj.protocol_id},
        {"productId", obj.terminal_product_id},
        {"channel", obj.channel},
        {"comType", obj.com_type},
        {"comIndex", obj.com_index},
        {"slaveAddr", obj.slave_addr},
        {"nodeId",obj.node_id},
        {"parentId", obj.parent_id},
        {"port", obj.port},
        {"securityDataUnit", obj.security_data_unit},
        {"securityVerifyUnit", obj.security_verify_unit}
    };
}
//
void from_json(const json& j, TERMINAL_DEVICE& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("protocolId").get_to(obj.protocol_id);
    j.at("channel").get_to(obj.channel);
    j.at("comType").get_to(obj.com_type);
    j.at("comIndex").get_to(obj.com_index);
    j.at("slaveAddr").get_to(obj.slave_addr);
    j.at("port").get_to(obj.port);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int64_t>(j, "productId", obj.terminal_product_id);
    json_to_obj<std::string>(j, "slaveAddr", obj.slave_addr);
    json_to_obj<std::string>(j, "nodeId", obj.node_id);
    json_to_obj<int>(j, "comType", obj.com_type);
    json_to_obj<int64_t>(j, "comIndex",  obj.com_index);
    json_to_obj<int64_t>(j, "parentId",  obj.parent_id);
    json_to_obj<decltype(obj.security_data_unit)>(j, "securityDataUnit",  obj.security_data_unit);
    json_to_obj<decltype(obj.security_verify_unit)>(j, "securityVerifyUnit",  obj.security_verify_unit);
}
//
void to_json(json& j, const std::list<TERMINAL_DEVICE>& objs) {
    for(auto dev = objs.begin(); dev != objs.end(); dev++){
        j.push_back(*dev);
    }
}
//
void from_json(const json& j, std::list<TERMINAL_DEVICE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        TERMINAL_DEVICE terminal_dev = *jj;
        objs.push_back(terminal_dev);
    }
}
//modbus
void to_json(json& j, const MODBUS_POINT& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"regAddr", obj.reg_addr},
        {"byteOrder", obj.byte_order},
        {"funcCode", obj.func_code},
        {"bitPos", obj.bit_pos},
        {"bitSize", obj.bit_size},
        {"dataType", obj.data_type},
        {"decimal", obj.decimal},
        {"formula", obj.formula},
        {"regCount", obj.reg_count},
        {"protocolId", obj.protocol_id},
        {"scale", obj.scale},
        {"thresholdCount", obj.threshold_count},
        {"unit", obj.unit},
        {"isAlarm", obj.is_alarm},
        {"lowerLimit", obj.lower_limit},
        {"lowerDesc", obj.lower_desc},
        {"upperLimit", obj.upper_limit},
        {"upperDesc", obj.upper_desc}
    };
}
//
void from_json(const json& j, MODBUS_POINT& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("protocolId").get_to(obj.protocol_id);
    j.at("regAddr").get_to(obj.reg_addr);
    j.at("bitPos").get_to(obj.bit_pos);
    j.at("dataType").get_to(obj.data_type);
    j.at("decimal").get_to(obj.decimal);
    //
    j.at("formula").get_to(obj.formula);
    j.at("regCount").get_to(obj.reg_count);
    j.at("scale").get_to(obj.scale);
    //j.at("thresholdCount").get_to(obj.threshold_count);
    //j.at("unit").get_to(obj.unit);
    j.at("lowerLimit").get_to(obj.lower_limit);
    j.at("lowerDesc").get_to(obj.lower_desc);
    j.at("upperLimit").get_to(obj.upper_limit);
    j.at("upperDesc").get_to(obj.upper_desc);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<std::string>(j, "byteOrder", obj.byte_order);
    json_to_obj<int>(j, "regAddr", obj.reg_addr);
    json_to_obj<int>(j, "bitPos", obj.bit_pos);
    json_to_obj<int>(j, "bitSize", obj.bit_size);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "decimal", obj.decimal);
    json_to_obj<std::string>(j, "formula", obj.formula);
    /*
    <option value="1">16位整型(有符号)</option>
    <option value="2">16位整型(无符号)</option>
    <option value="3">32位整型(有符号)</option>
    <option value="4">32位整型(无符号)</option>
    <option value="5">浮点型</option>
    <option value="6">开关量</option>
    */
    if(obj.data_type == 1){
        obj.reg_count = 1;
    }else if(obj.data_type == 2){
        obj.reg_count = 1;
    }else if(obj.data_type == 3){
        obj.reg_count = 2;
    }else if(obj.data_type == 4){
        obj.reg_count = 2;
    }else if(obj.data_type == 5){
        obj.reg_count = 2;
    }else if(obj.data_type == 6){
        obj.reg_count = 1;
    }else{
        json_to_obj<int>(j, "regCount", obj.reg_count);
    }
    json_to_obj<int>(j, "funcCode", obj.func_code);
    json_to_obj<int>(j, "thresholdCount", obj.threshold_count);
    json_to_obj<double>(j, "scale", obj.scale);
    json_to_obj<int>(j, "isAlarm", obj.is_alarm);
    json_to_obj<double>(j, "lowerLimit", obj.lower_limit);
    json_to_obj<std::string>(j, "lowerDesc", obj.lower_desc);
    json_to_obj<double>(j, "upperLimit", obj.upper_limit);
    json_to_obj<std::string>(j, "upperDesc", obj.upper_desc);
}
//
void to_json(json& j, const std::list<MODBUS_POINT>& objs) {
    for(auto point = objs.begin(); point != objs.end(); point++){
        j.push_back(*point);
    }
}
//
void from_json(const json& j, std::list<MODBUS_POINT>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        MODBUS_POINT point = *jj;
        objs.push_back(point);
    }
}
//D/LT645
void to_json(json& j, const DLT645_POINT& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"blockNo", obj.block_no},
        {"blockSize",obj.block_size},
        {"di", obj.di},
        {"pos",obj.pos},
        {"encode",obj.encode},
        {"byteOrder",obj.byte_Order},
        {"datetimeFormat",obj.datetime_format},
        {"dataType", obj.data_type},
        {"dataSize", obj.data_size},
        {"decimal", obj.decimal},
        {"isAlarm",obj.isAlarm},
        {"formula", obj.formula},
        {"protocolId", obj.protocol_id},
        {"scale", obj.scale},
        {"unit", obj.unit},
        {"thresholdCount", obj.threshold_count},
        {"lowerLimit", obj.lower_limit},
        {"lowerDesc", obj.lower_desc},
        {"upperLimit", obj.upper_limit},
        {"upperDesc", obj.upper_desc}
    };
}
//
void from_json(const json& j, DLT645_POINT& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("protocolId").get_to(obj.protocol_id);
    j.at("blockNo").get_to(obj.block_no);
    j.at("di").get_to(obj.di);
    j.at("dataType").get_to(obj.data_type);
    j.at("decimal").get_to(obj.decimal);
    //
    j.at("thresholdCount").get_to(obj.threshold_count);
    j.at("formula").get_to(obj.formula);
    j.at("scale").get_to(obj.scale);
    j.at("unit").get_to(obj.unit);
    j.at("lowerLimit").get_to(obj.lower_limit);
    j.at("lowerDesc").get_to(obj.lower_desc);
    j.at("upperLimit").get_to(obj.upper_limit);
    j.at("upperDesc").get_to(obj.upper_desc);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int>(j, "blockNo", obj.block_no);
    json_to_obj<int>(j, "blockSize", obj.block_size);
    json_to_obj<std::string>(j, "di", obj.di);
    json_to_obj<int>(j, "pos", obj.pos);
    json_to_obj<int>(j, "encode", obj.encode);
    json_to_obj<int>(j, "byte_order", obj.byte_Order);
    json_to_obj<std::string>(j, "datetimeFormat", obj.datetime_format);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "dataSize", obj.data_size);
    json_to_obj<int>(j, "decimal", obj.decimal);
    json_to_obj<int>(j, "isAlarm", obj.isAlarm);
    json_to_obj<std::string>(j, "formula", obj.formula);
    json_to_obj<double>(j, "scale", obj.scale);
    json_to_obj<int>(j, "thresholdCount", obj.threshold_count);
    json_to_obj<std::string>(j, "unit", obj.unit);
    json_to_obj<double>(j, "lowerLimit", obj.lower_limit);
    json_to_obj<std::string>(j, "lowerDesc", obj.lower_desc);
    json_to_obj<double>(j, "upperLimit", obj.upper_limit);
    json_to_obj<std::string>(j, "upperDesc", obj.upper_desc);

}
//
void to_json(json& j, const std::list<DLT645_POINT>& objs) {
    for(auto point = objs.begin(); point != objs.end(); point++){
        j.push_back(*point);
    }
}
//
void from_json(const json& j, std::list<DLT645_POINT>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT645_POINT point = *jj;
        objs.push_back(point);
    }
}
//D/LT698
void to_json(json& j, const DLT698_POINT& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"subId", obj.sub_id},
        {"oad", obj.oad},
        {"dataType", obj.data_type},
        {"decimal", obj.decimal},
        {"formula", obj.formula},
        {"protocolId", obj.protocol_id},
        {"scale", obj.scale},
        {"thresholdCount", obj.threshold_count},
        {"unit", obj.unit},
        {"isAlarm", obj.is_alarm},
        {"lowerLimit", obj.lower_limit},
        {"lowerDesc", obj.lower_desc},
        {"upperLimit", obj.upper_limit},
        {"upperDesc", obj.upper_desc}
    };
}
//
void from_json(const json& j, DLT698_POINT& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("protocolId").get_to(obj.protocol_id);
    j.at("subId").get_to(obj.sub_id);
    j.at("oad").get_to(obj.oad);
    j.at("dataType").get_to(obj.data_type);
    j.at("decimal").get_to(obj.decimal);
    //
    j.at("formula").get_to(obj.formula);
    j.at("scale").get_to(obj.scale);
    j.at("thresholdCount").get_to(obj.threshold_count);
    j.at("unit").get_to(obj.unit);
    j.at("lowerLimit").get_to(obj.lower_limit);
    j.at("lowerDesc").get_to(obj.lower_desc);
    j.at("upperLimit").get_to(obj.upper_limit);
    j.at("upperDesc").get_to(obj.upper_desc);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int>(j, "subId", obj.sub_id);
    json_to_obj<std::string>(j, "oad", obj.oad);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "decimal", obj.decimal);
    json_to_obj<std::string>(j, "formula", obj.formula);
    json_to_obj<double>(j, "scale", obj.scale);
    json_to_obj<int>(j, "thresholdCount", obj.threshold_count);
    json_to_obj<std::string>(j, "unit", obj.unit);
    json_to_obj<int>(j, "isAlarm", obj.is_alarm);
    json_to_obj<double>(j, "lowerLimit", obj.lower_limit);
    json_to_obj<std::string>(j, "lowerDesc", obj.lower_desc);
    json_to_obj<double>(j, "upperLimit", obj.upper_limit);
    json_to_obj<std::string>(j, "upperDesc", obj.upper_desc);
}
//
void to_json(json& j, const std::list<DLT698_POINT>& objs) {
    for(auto point = objs.begin(); point != objs.end(); point++){
        j.push_back(*point);
    }
}
//
void from_json(const json& j, std::list<DLT698_POINT>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT698_POINT point = *jj;
        objs.push_back(point);
    }
}
//
void to_json(json& j, const MODBUS_REQUEST& obj) {
    j = json{
        {"id", obj.id},
        {"regAddr", obj.reg_addr},
        {"funcCode", obj.func_code},
        {"regCount", obj.reg_count},
        {"requestSn", obj.request_sn},
        {"protocolId", obj.protocol_id},
        {"timeout", obj.timeout}
    };
}
//
void from_json(const json& j, MODBUS_REQUEST& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("regAddr").get_to(obj.reg_addr);
    j.at("funcCode").get_to(obj.func_code);
    j.at("regCount").get_to(obj.reg_count);
    j.at("requestSn").get_to(obj.request_sn);
    j.at("protocolId").get_to(obj.protocol_id);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int>(j, "regAddr", obj.reg_addr);
    json_to_obj<int>(j, "funcCode", obj.func_code);
    json_to_obj<int>(j, "regCount", obj.reg_count);
    json_to_obj<int>(j, "requestSn", obj.request_sn);
    json_to_obj<int>(j, "timeout", obj.timeout);
}
//
void to_json(json& j, const std::list<MODBUS_REQUEST>& objs) {
    for(auto request = objs.begin(); request != objs.end(); request++){
        j.push_back(*request);
    }
}
//
void from_json(const json& j, std::list<MODBUS_REQUEST>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        MODBUS_REQUEST request = *jj;
        objs.push_back(request);
    }
}
//
void to_json(json& j, const DLT645_REQUEST& obj) {
    j = json{
        {"id", obj.id},
        {"di", obj.di},
        {"funcCode", obj.func_code},
        {"requestSn", obj.request_sn},
        {"protocolId", obj.protocol_id},
        {"timeout", obj.timeout}
    };
}
//
void from_json(const json& j, DLT645_REQUEST& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("di").get_to(obj.di);
    j.at("requestSn").get_to(obj.request_sn);
    j.at("protocolId").get_to(obj.protocol_id);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "di", obj.di);
    json_to_obj<int>(j, "funcCode", obj.func_code);
    json_to_obj<int>(j, "requestSn", obj.request_sn);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int>(j, "timeout", obj.timeout);
}
//
void to_json(json& j, const std::list<DLT645_REQUEST>& objs) {
    for(auto request = objs.begin(); request != objs.end(); request++){
        j.push_back(*request);
    }
}
//
void from_json(const json& j, std::list<DLT645_REQUEST>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT645_REQUEST request = *jj;
        objs.push_back(request);
    }
}
//
void to_json(json& j, const DLT698_REQUEST& obj) {
    j = json{
        {"id", obj.id},
        {"oad", obj.oad},
        {"funcCode", obj.func_code},
        {"requestSn", obj.request_sn},
        {"protocolId", obj.protocol_id},
        {"timeout", obj.timeout}
    };
}
//
void from_json(const json& j, DLT698_REQUEST& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("oad").get_to(obj.oad);
    j.at("funcCode").get_to(obj.func_code);
    j.at("requestSn").get_to(obj.request_sn);
    j.at("protocolId").get_to(obj.protocol_id);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "oad", obj.oad);
    json_to_obj<int>(j, "funcCode", obj.func_code);
    json_to_obj<int>(j, "requestSn", obj.request_sn);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<int>(j, "timeout", obj.timeout);
}
//
void to_json(json& j, const std::list<DLT698_REQUEST>& objs) {
    for(auto request = objs.begin(); request != objs.end(); request++){
        j.push_back(*request);
    }
}
//
void from_json(const json& j, std::list<DLT698_REQUEST>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT698_REQUEST request = *jj;
        objs.push_back(request);
    }
}
//
void to_json(json& j, const MODBUS_COMMAND& obj) {
	int v = stoi(obj.def_value);
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"protocolId", obj.protocol_id},
        {"dataType", obj.data_type},
        {"funcCode", obj.func_code},
        {"regAddr", obj.reg_addr},
        {"regCount", obj.reg_count},
        {"formula", obj.formula},
        {"decimal", obj.decimal},
        {"scale", obj.scale},
        {"timeout", obj.timeout},
        {"defValue", v},
        {"byteOrder", obj.byte_order}
    };
}
//
void from_json(const json& j, MODBUS_COMMAND& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("name").get_to(obj.name);
    j.at("protocolId").get_to(obj.protocol_id);
    j.at("dataType").get_to(obj.data_type);
    j.at("funcCode").get_to(obj.func_code);
    j.at("regAddr").get_to(obj.reg_addr);
    j.at("value").get_to(obj.def_value);
    j.at("byteOrder").get_to(obj.byte_order);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "funcCode", obj.func_code);
    json_to_obj<int>(j, "regAddr", obj.reg_addr);
    if(obj.data_type == 1){
        obj.reg_count = 1;
    }else if(obj.data_type == 2){
        obj.reg_count = 1;
    }else if(obj.data_type == 3){
        obj.reg_count = 2;
    }else if(obj.data_type == 4){
        obj.reg_count = 2;
    }else if(obj.data_type == 5){
        obj.reg_count = 2;
    }else if(obj.data_type == 6){
        obj.reg_count = 1;
    }else{
        json_to_obj<int>(j, "regCount", obj.reg_count);
    }
    json_to_obj<std::string>(j, "formula", obj.formula);
    json_to_obj<int>(j, "decimal", obj.decimal);
    json_to_obj<double>(j, "scale", obj.scale);
    json_to_obj<int>(j, "timeout", obj.timeout);
    json_to_obj<std::string>(j, "defValue", obj.def_value);
    json_to_obj<std::string>(j, "byteOrder", obj.byte_order);
}
//
void to_json(json& j, const std::list<MODBUS_COMMAND>& objs) {
    for(auto cmd = objs.begin(); cmd != objs.end(); cmd++){
        j.push_back(*cmd);
    }
}
//
void from_json(const json& j, std::list<MODBUS_COMMAND>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        MODBUS_COMMAND cmd = *jj;
        objs.push_back(cmd);
    }
}
//
void to_json(json& j, const TASK& obj) {
    j = json{
        {"id", obj.id},
        {"name", obj.name},
        {"type", obj.type},
        {"startTime", obj.start_time},
        {"intervalTime", obj.interval_time},
        {"priority", obj.priority}
    };
}
//
void from_json(const json& j, TASK& obj) {
#if 0
    j.at("id").get_to(obj.id);
    //j.at("name").get_to(obj.name);
    j.at("type").get_to(obj.type);
    //j.at("startTime").get_to(obj.start_time);
    j.at("intervalTime").get_to(obj.interval_time);
    j.at("priority").get_to(obj.priority);
#endif

    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "type", obj.type);
    //需要变更业务
    json_to_obj<std::string>(j, "startTime", obj.start_time);
    json_to_obj<uint64_t>(j, "intervalTime", obj.interval_time);
    json_to_obj<int>(j, "priority", obj.priority);
}
//
void to_json(json& j, const std::list<TASK>& objs) {
    for(auto task = objs.begin(); task != objs.end(); task++){
        j.push_back(*task);
    }
}
//
void from_json(const json& j, std::list<TASK>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        TASK task = *jj;
        objs.push_back(task);
    }
}
//
void to_json(json& j, const TASK_ITEM& obj) {
    j = json{
        {"id", obj.id},
        {"terminalDeviceId", obj.terminal_dev_id},
        {"timerId", obj.task_id},
        {"taskItemId", obj.task_item_id}
    };
}
//
void from_json(const json& j, TASK_ITEM& obj) {
#if 0
    j.at("id").get_to(obj.id);
    j.at("terminalDeviceId").get_to(obj.terminal_dev_id);
    j.at("timerId").get_to(obj.task_id);
    j.at("taskItemId").get_to(obj.task_item_id);
#endif
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "terminalDeviceId", obj.terminal_dev_id);
    json_to_obj<int64_t>(j, "timerId", obj.task_id);
    json_to_obj<int64_t>(j, "taskItemId", obj.task_item_id);
}
//
void to_json(json& j, const std::list<TASK_ITEM>& objs) {
    for(auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}
//
void from_json(const json& j, std::list<TASK_ITEM>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        TASK_ITEM item = *jj;
        objs.push_back(item);
    }
}
void to_json(json& j, const DLT645_COMMAND& obj)
{
    j = json{
        {"id",obj.id},
        {"protocolId",obj.protocol_id },
        {"funcCode",obj.func_code},
        {"name",obj.name},
        {"di",obj.di},
        {"defValue",obj.def_value},
        {"passwd",obj.passwd},
        {"oprcode",obj.oprcode},
        {"timeout",obj.timeout},
        {"codingType",obj.coding_type},
        {"dataType",obj.data_type},
        {"dataSize",obj.data_size},
        {"byteOrder",obj.byte_order},
        {"scale",obj.scale},
        {"decimal", obj.decimal},
        {"formula",obj.formula},
        {"description",obj.description}
    };
}
void from_json(const json& j, DLT645_COMMAND& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<uint8_t>(j, "funcCode", obj.func_code);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<std::string>(j, "di", obj.di);
    json_to_obj<std::string>(j, "defValue", obj.def_value);
    json_to_obj<std::string>(j, "passwd", obj.passwd);
    json_to_obj<std::string>(j, "oprcode", obj.oprcode);
    json_to_obj<int>(j, "codingType", obj.coding_type);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "dataSize", obj.data_size);
    json_to_obj<int>(j, "byteOrder", obj.byte_order);
    json_to_obj<float>(j, "scale", obj.scale);
    json_to_obj<int>(j,"decimal",obj.decimal);
    json_to_obj<std::string>(j, "formula", obj.formula);
    json_to_obj<int>(j, "timeout", obj.timeout);
    json_to_obj<std::string>(j, "description", obj.description);
}

void to_json(json& j, const std::list<DLT645_COMMAND>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}

void from_json(const json& j, std::list<DLT645_COMMAND>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        DLT645_COMMAND item = *jj;
        objs.push_back(item);
    }
}

void to_json(json& j, const ST_IEC104_RULE& obj)
{
    j = json{
        { "id", obj.id },
        { "name", obj.name },
        { "mode", obj.mode},
        { "asdu_addr", obj.asdu_addr },
        { "description", obj.description },
        { "securityPolicy", obj.security_policy },
        { "masterAddr", obj.master_addr },
        { "masterPort", obj.master_port },
        { "createTime", obj.create_time },
        { "enable", obj.enable },
        { "registration", obj.regsiter_code}
    };
}
void from_json(const json& j, ST_IEC104_RULE& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int>(j, "mode", obj.mode);
    json_to_obj<int>(j, "asduAddr", obj.asdu_addr);
    json_to_obj<std::string>(j, "description", obj.description);
    json_to_obj<int>(j, "securityPolicy", obj.security_policy);
    json_to_obj<std::string>(j, "masterAddr", obj.master_addr);
    json_to_obj<int>(j, "masterPort", obj.master_port);
    json_to_obj<std::string>(j,"createTime",obj.create_time);
    json_to_obj<int>(j, "enable", obj.enable);
    json_to_obj<std::string>(j, "registration", obj.regsiter_code);
}
void to_json(json& j, const std::list<ST_IEC104_RULE>& objs)
{
    auto item = objs.begin();
    if(item == objs.end()){
        return ;
    }
    j.push_back(*item);
}
void from_json(const json& j, std::list<ST_IEC104_RULE>& objs)
{
    ST_IEC104_RULE item = j;
    objs.push_back(item);
}
void to_json(json& j, const ST_104_INFO_OBJECT& obj)
{
    j = json{
        { "id", obj.id },
        { "terminalDevId", obj.terminal_id },
        { "pointId", obj.point_id },
        { "infoAddr", obj.info_addr },
        { "infoType", obj.info_type },
        { "deadZone", obj.deadzone },
        { "type",obj.type}
    };

}
void from_json(const json& j, ST_104_INFO_OBJECT& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "terminalDevId", obj.terminal_id);
    json_to_obj<int64_t>(j, "pointId", obj.point_id);
    json_to_obj<int>(j, "infoAddr", obj.info_addr);
    json_to_obj<int>(j, "infoType", obj.info_type);
    json_to_obj<double>(j, "deadZone", obj.deadzone);
    json_to_obj<int>(j, "type", obj.type);
}
void to_json(json& j, const std::list<ST_104_INFO_OBJECT>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}
void from_json(const json& j, std::list<ST_104_INFO_OBJECT>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        ST_104_INFO_OBJECT item = *jj;
        objs.push_back(item);
    }
}

void to_json(json& j, const ST_DLT645_FREEZE_TIME& obj)
{
    j = {
        {"id", obj.id },
        {"protocolId",obj.protocol_id},
        {"timeOrDi",obj.time_or_di},
        {"type",obj.type},
        {"blockCount",obj.block_count},
        {"freezeInterval",obj.freeze_interval},
        {"freezeType", obj.freeze_type},
        {"timeout", obj.time_out},
        {"description", obj.description}
    };
}

void from_json(const json& j, ST_DLT645_FREEZE_TIME& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<int64_t>(j, "protocolId", obj.protocol_id);
    json_to_obj<std::string>(j, "timeOrDi", obj.time_or_di);
    json_to_obj<int>(j, "type", obj.type);
    json_to_obj<int>(j, "blockCount", obj.block_count);
    json_to_obj<int>(j, "freezeInterval", obj.freeze_interval);
    json_to_obj<std::string>(j, "freezeType", obj.freeze_type);
    json_to_obj<int>(j, "timeout", obj.time_out);
    json_to_obj<std::string>(j, "description", obj.description);
}

void to_json(json& j, const std::list<ST_DLT645_FREEZE_TIME>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}

void from_json(const json& j, std::list<ST_DLT645_FREEZE_TIME>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        ST_DLT645_FREEZE_TIME item = *jj;
        objs.push_back(item);
    }
}

void to_json(json& j, const ST_DLT645_FREEZE_REQUEST & obj)
{
    j = {
        {"id",obj.id},
        {"name",obj.name},
        {"freezeTimeId",obj.freeze_time_id},
        {"di",obj.di},
        {"timeout",obj.timeout},
        {"description",obj.description}
    };
}

void from_json(const json& j, ST_DLT645_FREEZE_REQUEST& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<int64_t>(j, "freezeTimeId", obj.freeze_time_id);
    json_to_obj<std::string>(j, "di", obj.di);
    json_to_obj<int>(j, "timeout", obj.timeout);
    json_to_obj<std::string>(j, "description", obj.description);
}

void to_json(json& j, const std::list<ST_DLT645_FREEZE_REQUEST>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}

void from_json(const json& j, std::list<ST_DLT645_FREEZE_REQUEST>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        ST_DLT645_FREEZE_REQUEST item = *jj;
        objs.push_back(item);
    }
}
//
void to_json(json& j, const DLT698_RUN_PARAMS& obj) {
    //串口模式参数
    j = json{
        {"id", obj.id},
        {"enable", obj.enable},
        {"mode", obj.mode},
        {"sa", obj.sa},
        {"masterAddr", obj.master_addr},
        {"masterPort", obj.master_port},
        {"securityPolicy", obj.security_policy},
        {"heartbeatInterval", obj.heartbeat_interval}
    };
}

void from_json(const json& j, DLT698_RUN_PARAMS& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.enable)>(j, "enable", obj.enable);
    json_to_obj<decltype(obj.mode)>(j, "mode", obj.mode);
    json_to_obj<decltype(obj.sa)>(j, "sa", obj.sa);
    json_to_obj<decltype(obj.master_addr)>(j, "masterAddr", obj.master_addr);
    json_to_obj<decltype(obj.master_port)>(j, "masterPort", obj.master_port);
    json_to_obj<decltype(obj.security_policy)>(j, "securityPolicy", obj.security_policy);
    json_to_obj<decltype(obj.heartbeat_interval)>(j, "heartbeatInterval", obj.heartbeat_interval);
}

void to_json(json& j, const std::list<DLT698_RUN_PARAMS>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}

void from_json(const json& j, std::list<DLT698_RUN_PARAMS>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        DLT698_RUN_PARAMS item = *jj;
        objs.push_back(item);
    }
}
//
void to_json(json& j, const ST_TERMINAL_PRODUCT& obj)
{
    j = {
        { "id", obj.id },
        { "name", obj.name },
        { "model", obj.model },
        { "producer", obj.producer },
        { "manuId",obj.manuId},
        { "supportModbus", obj.support_modbus },
        { "support645", obj.support_645 },
        { "support698", obj.support_698 },
        { "moduleName",obj.model_name}
    };
}

void from_json(const json& j, ST_TERMINAL_PRODUCT& obj)
{
    json_to_obj<int64_t>(j, "id", obj.id);
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<std::string>(j, "model", obj.model);
    json_to_obj<std::string>(j, "producer", obj.producer);
    json_to_obj<std::string>(j, "manuId", obj.manuId);
    json_to_obj<int>(j, "support_modbus", obj.support_modbus);
    json_to_obj<int>(j, "support_645", obj.support_645);
    json_to_obj<int>(j, "support_698", obj.support_698);
    json_to_obj<std::string>(j,"moduleName",obj.model_name);
}

void to_json(json& j, const std::list<ST_TERMINAL_PRODUCT>& objs)
{
    for (auto item = objs.begin(); item != objs.end(); item++){
        j.push_back(*item);
    }
}
void from_json(const json& j, std::list<ST_TERMINAL_PRODUCT>& objs)
{
    for (auto jj = j.begin(); jj != j.end(); jj++){
        ST_TERMINAL_PRODUCT item = *jj;
        objs.push_back(item);
    }
}

//
void to_json(json& j, const CMD_REQUEST_DATA& obj) {
    //串口模式参数
    j = json{
            {"name", obj.name},
            {"value", obj.value},
            {"dataType", obj.data_type},
            {"order", obj.order}
        };
}

void from_json(const json& j, CMD_REQUEST_DATA& obj) {
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<std::string>(j, "value", obj.value);
    json_to_obj<int>(j, "dataType", obj.data_type);
    json_to_obj<int>(j, "order", obj.order);
}

void to_json(json& j, const std::list<CMD_REQUEST_DATA>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<CMD_REQUEST_DATA>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        CMD_REQUEST_DATA data = *jj;
        objs.push_back(data);
    }
}

void to_json(json& j, const CMD_REQUEST& obj) {
    //串口模式参数
    j = json{
            {"dev", obj.dev},
            {"commandType", obj.command_type},
            {"token", obj.token},
            {"timestamp", obj.timestamp},
            {"params", obj.params}
        };
}

void from_json(const json& j, CMD_REQUEST& obj) {
    json_to_obj<std::string>(j, "dev", obj.dev);
    json_to_obj<int64_t>(j, "token", obj.token);
    json_to_obj<std::string>(j, "commandType", obj.command_type);
    json_to_obj<uint64_t>(j, "timestamp", obj.timestamp);
    json_to_obj<std::list<CMD_REQUEST_DATA>>(j, "params", obj.params);
}

void to_json(json& j, const DLT698_MAPPING_POINT_TYPE& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"oad", obj.oad},
            {"pointId", obj.point_id},
            {"terminalDevId", obj.terminal_id},
            {"type", obj.type}
        };
}

void from_json(const json& j, DLT698_MAPPING_POINT_TYPE& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.oad)>(j, "oad", obj.oad);
    json_to_obj<decltype(obj.point_id)>(j, "pointId", obj.point_id);
    json_to_obj<decltype(obj.terminal_id)>(j, "terminalDevId", obj.terminal_id);
    json_to_obj<decltype(obj.type)>(j, "type", obj.type);
}

void to_json(json& j, const std::list<DLT698_MAPPING_POINT_TYPE>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<DLT698_MAPPING_POINT_TYPE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        DLT698_MAPPING_POINT_TYPE data = *jj;
        objs.push_back(data);
    }
}

//联动条件
void to_json(json& j, const LINKAGE_CONDITION& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"deviceId", obj.device_id},
            {"deviceName", obj.device_name},
            {"pointId", obj.point_id},
            {"pointName", obj.point_name},
            {"op", obj.op},
            {"value", obj.value}
        };
}

void from_json(const json& j, LINKAGE_CONDITION& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.device_id)>(j, "deviceId", obj.device_id);
    json_to_obj<decltype(obj.device_name)>(j, "deviceName", obj.device_name);
    json_to_obj<decltype(obj.point_id)>(j, "pointId", obj.point_id);
    json_to_obj<decltype(obj.point_name)>(j, "pointName", obj.point_name);
    json_to_obj<decltype(obj.op)>(j, "op", obj.op);
    json_to_obj<decltype(obj.value)>(j, "value", obj.value);
}

void to_json(json& j, const std::list<LINKAGE_CONDITION>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<LINKAGE_CONDITION>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        LINKAGE_CONDITION data = *jj;
        objs.push_back(data);
    }
}
//联动动作
void to_json(json& j, const LINKAGE_ACTION& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"deviceId", obj.device_id},
            {"deviceName", obj.device_name},
            {"commandId", obj.cmd_id},
            {"commandName", obj.cmd_name},
            {"value", obj.value},
            {"delayTime", obj.delay},
            {"sort", obj.sort}
        };
}

void from_json(const json& j, LINKAGE_ACTION& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.device_id)>(j, "deviceId", obj.device_id);
    json_to_obj<decltype(obj.device_name)>(j, "deviceName", obj.device_name);
    json_to_obj<decltype(obj.cmd_id)>(j, "commandId", obj.cmd_id);
    json_to_obj<decltype(obj.cmd_name)>(j, "commandName", obj.cmd_name);
    json_to_obj<decltype(obj.value)>(j, "value", obj.value);
    json_to_obj<decltype(obj.delay)>(j, "delayTime", obj.delay);
    json_to_obj<decltype(obj.sort)>(j, "sort", obj.sort);
}

void to_json(json& j, const std::list<LINKAGE_ACTION>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<LINKAGE_ACTION>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        LINKAGE_ACTION data = *jj;
        objs.push_back(data);
    }
}
//联动规则
void to_json(json& j, const LINKAGE_MULTI_RULE& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"name", obj.name},
            {"isUsed", obj.is_used},
            {"conditions", obj.conditions},
            {"actions", obj.actions},
            {"description", obj.description}
        };
}

void from_json(const json& j, LINKAGE_MULTI_RULE& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.name)>(j, "name", obj.name);
    int isUse = false;
    json_to_obj<int>(j, "isUsed", isUse);
    obj.is_used = isUse;
    // json_to_obj<int>(j, "isUsed", obj.is_used);
    json_to_obj<decltype(obj.description)>(j, "description", obj.description);
    json_to_obj<std::list<LINKAGE_CONDITION>>(j, "conditions", obj.conditions);
    json_to_obj<std::list<LINKAGE_ACTION>>(j, "actions", obj.actions);
}
void to_json(json& j, const std::list<LINKAGE_MULTI_RULE>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<LINKAGE_MULTI_RULE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        LINKAGE_MULTI_RULE data = *jj;
        objs.push_back(data);
    }
}
//场景动作
void to_json(json& j, const SCENE_ACTION& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"deviceId", obj.device_id},
            {"deviceName", obj.device_name},
            {"commandId", obj.cmd_id},
            {"commandName", obj.cmd_name},
            {"value", obj.value},
            {"delayTime", obj.delay},
            {"sort", obj.sort}
        };
}

void from_json(const json& j, SCENE_ACTION& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.device_id)>(j, "deviceId", obj.device_id);
    json_to_obj<decltype(obj.device_name)>(j, "deviceName", obj.device_name);
    json_to_obj<decltype(obj.cmd_id)>(j, "commandId", obj.cmd_id);
    json_to_obj<decltype(obj.cmd_name)>(j, "commandName", obj.cmd_name);
    json_to_obj<decltype(obj.value)>(j, "value", obj.value);
    json_to_obj<decltype(obj.delay)>(j, "delayTime", obj.delay);
    json_to_obj<decltype(obj.sort)>(j, "sort", obj.sort);
}

void to_json(json& j, const std::list<SCENE_ACTION>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<SCENE_ACTION>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        SCENE_ACTION data = *jj;
        objs.push_back(data);
    }
}
//应用场景
void to_json(json& j, const SCENE& obj) {
    //串口模式参数
    j = json{
            {"id", obj.id},
            {"name", obj.name},
            {"isUsed", obj.is_used},
            {"actions", obj.actions},
            {"description", obj.description}
        };
}

void from_json(const json& j, SCENE& obj) {
    json_to_obj<decltype(obj.id)>(j, "id", obj.id);
    json_to_obj<decltype(obj.name)>(j, "name", obj.name);
    int isUse = false;
    json_to_obj<int>(j, "isUsed", isUse);
    obj.is_used = isUse;
    json_to_obj<decltype(obj.description)>(j, "description", obj.description);
    json_to_obj<std::list<SCENE_ACTION>>(j, "actions", obj.actions);
}
void to_json(json& j, const std::list<SCENE>& objs) {
    for(auto data = objs.begin(); data != objs.end(); data++){
        j.push_back(*data);
    }
}

void from_json(const json& j, std::list<SCENE>& objs) {
    for(auto jj = j.begin(); jj != j.end(); jj++){
        SCENE data = *jj;
        objs.push_back(data);
    }
}
//
void to_json(json& j, const CONFIG_ALL& obj) {
    auto edge_dev = obj.edge_devices.begin();
    if(edge_dev == obj.edge_devices.end()){
        j = json{
            {"dlt698DataType", obj.dlt698_data_type},
            {"dlt645DataType", obj.dlt645_data_type},
            {"dlt645Points", obj.dlt645_points},
            {"dlt645Requests", obj.dlt645_requests},
            {"dlt698Points", obj.dlt698_points},
            {"dlt698Requests", obj.dlt698_requests},
            {"modbusCommands", obj.modbus_commands},
            {"modbusPoints", obj.modbus_points},
            {"modbusRequests", obj.modbus_requests},
            {"protocolRules", obj.protocol_rules},
            {"taskItems", obj.task_items},
            {"timers", obj.tasks},
            {"terminalDevices", obj.terminal_devices},
            {"dlt645Commands", obj.dlt645_commands},
            {"iec104North", obj.iec104_rules},
            {"iec104cfgs", obj.lst104info_obj},
            {"freezeTime", obj.dlt645_freeze_times},
            {"freezeData", obj.dlt645_freeze_requests},
            {"terminalProducts", obj.terminal_products},
            {"linkageRules", obj.linkage_multi_rules},
            {"dlt698RunParams", obj.dlt698_run_params},
            {"scenes",obj.scenes}
        };
    }else{
        j = json{
            {"dlt698DataType", obj.dlt698_data_type},
            {"dlt645DataType", obj.dlt645_data_type},
            {"dlt645Points", obj.dlt645_points},
            {"dlt645Requests", obj.dlt645_requests},
            {"dlt698Points", obj.dlt698_points},
            {"dlt698Requests", obj.dlt698_requests},
            {"edgeDevice", *edge_dev},
            {"modbusCommands", obj.modbus_commands},
            {"modbusPoints", obj.modbus_points},
            {"modbusRequests", obj.modbus_requests},
            {"protocolRules", obj.protocol_rules},
            {"taskItems", obj.task_items},
            {"timers", obj.tasks},
            {"terminalDevices", obj.terminal_devices},
            {"dlt645Commands", obj.dlt645_commands},
            {"iec104North", obj.iec104_rules},
            {"iec104cfgs", obj.lst104info_obj},
            {"freezeTime", obj.dlt645_freeze_times},
            {"freezeData", obj.dlt645_freeze_requests},
            {"terminalProducts",obj.terminal_products},
            {"linkageRules",obj.linkage_multi_rules},
            {"dlt698RunParams",obj.dlt698_run_params},
            {"scenes",obj.scenes}
        };
    }
}
//
void from_json(const json& j, CONFIG_ALL& obj) {
    if(j.contains("params")){
        auto params_json = j.at("params");
        if(params_json.contains("dlt698DataType")){
            CLogger::getInstance()->log_d("json parse dlt698_data_type ");
            json_to_obj<std::list<DLT698_DATA_TYPE>>(params_json, "dlt698DataType", obj.dlt698_data_type);
        }
        //
        if(params_json.contains("dlt645DataType")){
            CLogger::getInstance()->log_d("json parse dlt645_data_type ");
            json_to_obj<std::list<DLT645_DATA_TYPE>>(params_json, "dlt645DataType", obj.dlt645_data_type);
        }
        //
        if(params_json.contains("dlt645Points")){
            CLogger::getInstance()->log_d("json parse dlt645_points");
            json_to_obj<std::list<DLT645_POINT>>(params_json, "dlt645Points", obj.dlt645_points);
        }
        //
        if(params_json.contains("dlt645Requests")){
            CLogger::getInstance()->log_d("json parse dlt645_requests");
            json_to_obj<std::list<DLT645_REQUEST>>(params_json, "dlt645Requests", obj.dlt645_requests);
        }
        if(params_json.contains("dlt698Points")){
            CLogger::getInstance()->log_d("json parse dlt698_points");
            json_to_obj<std::list<DLT698_POINT>>(params_json, "dlt698Points", obj.dlt698_points);
        }
        if(params_json.contains("dlt698Requests")){
            CLogger::getInstance()->log_d("json parse dlt698_requests");
            json_to_obj<std::list<DLT698_REQUEST>>(params_json, "dlt698Requests", obj.dlt698_requests);
        }
        if(params_json.contains("edgeDevice")){
            CLogger::getInstance()->log_d("json parse edge_device");
            json_to_obj<std::list<EDGE_DEVICE>>(params_json, "edgeDevice", obj.edge_devices);
        }
        if(params_json.contains("modbusCommands")){
            CLogger::getInstance()->log_d("json parse modbus commands");
            json_to_obj<std::list<MODBUS_COMMAND>>(params_json, "modbusCommands", obj.modbus_commands);
        }
        if(params_json.contains("modbusPoints")){
            CLogger::getInstance()->log_d("json parse modbus_points");
            json_to_obj<std::list<MODBUS_POINT>>(params_json, "modbusPoints", obj.modbus_points);
        }
        if(params_json.contains("modbusRequests")){
            CLogger::getInstance()->log_d("json parse modbus requests");
            json_to_obj<std::list<MODBUS_REQUEST>>(params_json, "modbusRequests", obj.modbus_requests);
        }
        if(params_json.contains("protocolRules")){
            CLogger::getInstance()->log_d("json parse protocol_rules");
            json_to_obj<std::list<PROTOCOL_RULE>>(params_json, "protocolRules", obj.protocol_rules);
        }
        if(params_json.contains("taskItems")){
            CLogger::getInstance()->log_d("json parse task items");
            json_to_obj<std::list<TASK_ITEM>>(params_json, "taskItems", obj.task_items);
        }
        if(params_json.contains("timers")){
            CLogger::getInstance()->log_d("json parse timers");
            json_to_obj<std::list<TASK>>(params_json, "timers", obj.tasks);
        }
        //
        if(params_json.contains("terminalDevices")){
            CLogger::getInstance()->log_d("json parse terminal_devices");
            json_to_obj<std::list<TERMINAL_DEVICE>>(params_json, "terminalDevices", obj.terminal_devices);
        }
        if (params_json.contains("dlt645Commands"))
        {
            CLogger::getInstance()->log_d("json parse dlt645 commands");
            json_to_obj<std::list<DLT645_COMMAND>>(params_json, "dlt645Commands", obj.dlt645_commands);
        }
        if (params_json.contains("iec104North"))
        {
            CLogger::getInstance()->log_d("json parse iec104 rules");
            json_to_obj<std::list<ST_IEC104_RULE>>(params_json, "iec104North", obj.iec104_rules);
        }
        if (params_json.contains("iec104cfgs"))
        {
            CLogger::getInstance()->log_d("json parse iec104 info objs");
            json_to_obj<std::list<ST_104_INFO_OBJECT>>(params_json, "iec104cfgs", obj.lst104info_obj);
        }
        if (params_json.contains("freezeTime"))
        {
            CLogger::getInstance()->log_d("json parse dlt645 freezetime objs");
            json_to_obj<std::list<ST_DLT645_FREEZE_TIME>>(params_json, "freezeTime", obj.dlt645_freeze_times);
        }
        if (params_json.contains("freezeData"))
        {
            CLogger::getInstance()->log_d("json parse dlt645 freezerequest objs");
            json_to_obj<std::list<ST_DLT645_FREEZE_REQUEST>>(params_json, "freezeData", obj.dlt645_freeze_requests);
        }
        if (params_json.contains("terminalProducts"))
        {
            CLogger::getInstance()->log_d("json parse terminal product objs");
            json_to_obj<std::list<ST_TERMINAL_PRODUCT>>(params_json, "terminalProducts",obj.terminal_products);
        }
		if (params_json.contains("dlt698RunParams")){
            CLogger::getInstance()->log_d("json parse dlt698 run params");
            json_to_obj<std::list<DLT698_RUN_PARAMS>>(params_json, "dlt698RunParams", obj.dlt698_run_params);
        }
        if(params_json.contains("linkageRules")){
            CLogger::getInstance()->log_d("json parse linkage_multi_rules");
            json_to_obj<std::list<LINKAGE_MULTI_RULE>>(params_json, "linkageRules", obj.linkage_multi_rules);
        }
        if(params_json.contains("scenes")){
            CLogger::getInstance()->log_d("json parse scenes");
            json_to_obj<std::list<SCENE>>(params_json, "scenes", obj.scenes);
        }

    }
}

//定值动作
void to_json(json& j, const SET_CONST_VALUE& obj) {
    //
    j = json{
            {"cmdId", obj.cmdId},
            {"value", obj.value}
        };
}

void from_json(const json& j, SET_CONST_VALUE& obj) {
    json_to_obj<decltype(obj.cmdId)>(j, "cmdId", obj.cmdId);
    json_to_obj<decltype(obj.value)>(j, "value", obj.value);
}

void to_json(json& j, const CMD_SET_CONST_VALUE& obj) {
    //
    j = json{
            {"command", obj.command},
            {"commandId", obj.commandId},
            {"commandType", obj.commandType},
            {"devId", obj.devId},
            {"timestamp", obj.timestamp},
            {"params", obj.params}
        };
}

void from_json(const json& j, CMD_SET_CONST_VALUE& obj) {
    json_to_obj<decltype(obj.command)>(j, "command", obj.command);
    json_to_obj<decltype(obj.commandId)>(j, "commandId", obj.commandId);
    json_to_obj<decltype(obj.commandType)>(j, "commandType", obj.commandType);
    json_to_obj<decltype(obj.devId)>(j, "devId", obj.devId);
    json_to_obj<decltype(obj.timestamp)>(j, "timestamp", obj.timestamp);
    json_to_obj<decltype(obj.params)>(j, "params", obj.params);
}

#endif /*__JSON_TRANSLATOR_H__*/